package com.blb.orderservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.common.entity.Order;
import com.blb.common.entity.Product;
import com.blb.common.entity.ResponseResult;
import com.blb.common.feign.ProductServiceClient;
import com.blb.common.util.MyEurekaClient;
import com.blb.orderservice.mapper.OrderMapper;
import com.blb.orderservice.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

//    //注入http客户端
//    @Autowired
//    private RestTemplate restTemplate;

//    @Autowired
//    private MyEurekaClient myEurekaClient;

    @Autowired
    private ProductServiceClient productServiceClient;

    @Override
    public Order getOrderById(Long id){
        Order order = orderMapper.selectById(id);
        //通过网络连接商品服务调用查询商品的接口,写死ip和端口容易出错,如果注册到eureka上，可以使用服务名代替ip和端点
//        Product result = restTemplate.getForObject(
//                "http://product-service/product/" + order.getProductId(), Product.class);
        Product result = productServiceClient.getProductById(order.getProductId());
        order.setProduct(result);
//        try {
//            String json = myEurekaClient.get("product-service", "/product/" + order.getProductId());
//            Product product = new ObjectMapper().readValue(json, Product.class);
//            order.setProduct(product);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
        return order;
    }
}
